package com.config.service.impl;

import cn.hutool.core.util.StrUtil;
import com.config.entity.TbSpecification;
import com.config.entity.TbSpecificationExample;
import com.config.entity.TbSpecificationOption;
import com.config.entity.TbSpecificationOptionExample;
import com.config.mapper.TbSpecificationMapper;
import com.config.mapper.TbSpecificationOptionMapper;
import com.config.service.TbSpecificationOptionService;
import com.config.util.PageResult;
import com.config.vo.TbSpecificationVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class TbSpecificationOptionServiceimpl implements TbSpecificationOptionService {


    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Override
    public List<TbSpecificationOption> findAll() {
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(null);
        tbSpecificationOptions.forEach(st -> {
            TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(st.getSpecId());
            st.setSpecName(tbSpecification.getSpecName());
        });
        return tbSpecificationOptions;
    }
    @Override
    public PageResult<TbSpecificationOption> findByPage(Integer page, Integer pageSize) {
        //1. 开始分页
        PageHelper.startPage(page,pageSize);
        // 2. 开始查询数据
        List<TbSpecificationOption> tbSpecifications = this.findAll();
        // 3. 将查询得到的数据转换为Page<Student>对象
        Page<TbSpecificationOption> tbSpecificationPage = (Page<TbSpecificationOption>) tbSpecifications;
        // 4. 转换为PageResult对象studentPage.getResult(),studentPage.getTotal()
        return new PageResult<>(tbSpecificationPage.getResult(),tbSpecificationPage.getTotal());

    }
    // 条件查询带分页功能
    @Override
    public PageResult<TbSpecificationOption> search(Integer page, Integer pageSize, TbSpecificationVO tbSpecificationVO) {
        // 1. 开始分页
        PageHelper.startPage(page, pageSize);
        // 2. 查询数据
        // 2.1 定义一次查询的实例
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        // 2.2 添加查询条件到查询实例中
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        if(tbSpecificationVO.getId()!=null&&tbSpecificationVO.getId() != 0){
            criteria.andSpecIdEqualTo(Long.valueOf(tbSpecificationVO.getId()));
        }

        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(example);
        tbSpecificationOptions.forEach(st -> {
            TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(st.getSpecId());
            st.setSpecName(tbSpecification.getSpecName());
        });


        // 2.4 将上面的students对象转换为List集合
        Page<TbSpecificationOption> tbSpecificationOptionPage = (Page<TbSpecificationOption>) tbSpecificationOptions;
        return new PageResult<>(tbSpecificationOptionPage.getResult(),tbSpecificationOptionPage.getTotal());

    }

    // 3. 添加学生
    @Override
    public boolean save(TbSpecificationOption tbSpecificationOption) {
        return tbSpecificationOptionMapper.insert(tbSpecificationOption) > 0;
    }
}
